<?php
/**
 * https://gitee.com/litephp
 * http://www.dazhetu.cn/
 * jay_fun 410136330@qq.com
 * Date: 2019/1/11
 * Time: 15:33
 */

namespace app\admin\controller\content;

use app\common\controller\BaseAdmin;
use app\common\model\content\Recharge as RechargeModel;
use LiteAdmin\Music163;
use think\db;

/**
 * @title 充值管理
 * Class Music
 * @package app\admin\controller\content
 */
class Recharge extends BaseAdmin
{
    /**
     * @title 列表页
     * @return mixed
     */
    public function index()
    {
        $db = RechargeModel::alias('r')
            ->join('content_user u','u.id = r.uid','left')
            ->join('content_pbank p','p.id = r.pt_id','left')
            ->field('r.*,u.uname,p.kaihuhang,p.kname,p.kahao')
            ->order('create_time desc');

        $search = $this->request->get();
        // 精准查询
        foreach (['status'] as $field){
            if (isset($search[$field]) && $search[$field] !== ''){
                $db->where('r.'.$field,'=', $search[$field]);
            }
        }
        // 模糊查询
        foreach (['uname'] as $field){
            if (isset($search[$field]) && $search[$field] !== ''){
                $db->whereLike('u.'.$field, "%{$search[$field]}%");
            }
        }
        return $this->_list($db, true, $search);
    }




    /**
     * @title 审核充值
     */
    public function change()
    {
        $id = $this->request->post('id');
        $state = $this->request->post('status');
         $this->tongguo($id,$state);

    }
    public function tongguo($id,$state){

       if($state == 1){
           db()->startTrans();
           $list = db('content_recharge')->where(array('id'=>$id))->find();
           $b = db('content_balance')->where(array('uid'=>$list['uid']))->find();
           $balance = db('content_balance')->where(array('uid'=>$list['uid']))->setInc('mineral',$list['num']);
           $arr = [
               'uid'=>$list['uid'],
               'content'=>'充值+'.$list['num'],
               'front'=>$b['mineral'],
               'after'=>$b['mineral']+$list['num'],
               'type'=>1,
               'create_time'=>time(),
               'sign' => 1,
               'number'=>$list['num']
           ];
           $log = db('content_balance_log')->insert($arr);
           $change =  $this->_change(new RechargeModel(), $id, ['status' => $state]);
           if($balance && $change && $log){
               db()->commit();
           }else{
               db()->rollback();
               $this->error('切换状态操作失败！');
           }

       }else{
           $this->_change(new RechargeModel(), $id, ['status' => $state]);
       }



    }
    /**
     * @title 删除充值记录
     */
    public function del()
    {
        $ids = $this->request->post('ids',false);
        !$ids && $this->error("缺少参数！");

        $this->_del(new RechargeModel(),$ids);
    }

}